package com.infitecs.eyas.boiler;

import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.model.SodaParameter;
import com.infitecs.eyas.boiler.model.Summary;
import com.infitecs.eyas.boiler.service.AirGasParameterService;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.boiler.service.SodaParameterService;
import com.infitecs.eyas.boiler.service.SummaryService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.step.service.StepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Demon on 2015/5/13.
 */
@Controller
@RequestMapping("/boiler")
public class BoilerController {
    @Autowired
    private AirGasParameterService airGasParameterService;

    @Autowired
    private EfficiencyService efficiencyService;

    @Autowired
    private SodaParameterService sodaParameterService;

    @Autowired
    private SummaryService summaryService;

    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;

    @RequestMapping(value = "/gasogene")
    public String getGasogenePage(HttpSession session, HttpServletRequest request) {
        String cId = (String) session.getAttribute("caseId");
        if (cId == null) {
            return "boiler/gasogene";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "boiler/gasogene";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        SodaParameter sodaParameter = sodaParameterService.getSodaParameter(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("sodaParameter", sodaParameter);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "boiler/gasogene";
    }

    @RequestMapping(value = "/airgas")
    public String getAirGasPage(HttpServletRequest request) {

        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "boiler/airgas";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "boiler/airgas";
        }
        step = stepService.getGlStep(caseId);
        if (step < 7) {
            request.setAttribute("step", step);
            return "boiler/airgas";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("airGasParameter", airGasParameter);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "boiler/airgas";
    }



    @RequestMapping(value = "/efficiency")
    public String getEfficiencyPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "boiler/efficiency";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "boiler/efficiency";
        }
        step = stepService.getGlStep(caseId);
        if (step < 8) {
            request.setAttribute("step", step);
            return "boiler/efficiency";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        Efficiency efficiency = efficiencyService.getEfficiency(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("efficiency", efficiency);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "boiler/efficiency";
    }

    @RequestMapping(value = "/summary")
    public String getSummaryPage(HttpServletRequest request) throws InterruptedException {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "boiler/efficiency";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "boiler/summary";
        }
        step = stepService.getGlStep(caseId);
        if (step < 9) {
            request.setAttribute("step", step);
            return "boiler/summary";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        Summary summary = summaryService.getSummary(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("summary", summary);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "boiler/summary";
    }

}
